var wrestMsg = "";
var wrestFld = null;
var wrestFldDefaultColor = "";
//var wrestFldBackColor = "#ff3061";

// subject 获取属性值 return, 如果没有 tag的 name移交
function wrestItemname(fld)
{
    //return fld.getAttribute("title") ? fld.getAttribute("title") : ( fld.getAttribute("alt") ? fld.getAttribute("alt") : fld.name );
    var id = fld.getAttribute("id");
    var labels = document.getElementsByTagName("label");
    var el = null;

    for(i=0; i<labels.length; i++) {
        if(id == labels[i].htmlFor) {
            el = labels[i];
            break;
        }
    }

    if(el != null) {
        var text =  el.innerHTML.replace(/[<].*[>].*[<]\/+.*[>]/gi, "");

        if(text == '') {
            return fld.getAttribute("title") ? fld.getAttribute("title") : ( fld.getAttribute("placeholder") ? fld.getAttribute("placeholder") : fld.name );
        } else {
            return text;
        }
    } else {
        return fld.getAttribute("title") ? fld.getAttribute("title") : ( fld.getAttribute("placeholder") ? fld.getAttribute("placeholder") : fld.name );
    }
}

// 为了摆脱这两个空间
function wrestTrim(fld)
{
    return fld.value.trim();
}

// 重要输入检查
function wrestRequired(fld)
{
    if (wrestTrim(fld) == "") {
        if (wrestFld == null) {
            // 即使它是一个选择框，它也会检查所需的选择.
            wrestMsg = wrestItemname(fld) + " : 必不可少的 "+(fld.type=="select-one"?"选择":"输入")+"就这样..\n";
            wrestFld = fld;
        }
    }
}

// 金善勇 2006.3 - 电话号码(移动电话) 格式检查 : 123-123(4)-5678
function wrestTelNum(fld)
{
    if (!wrestTrim(fld)) return;

    var pattern = /^[0-9]{2,3}-[0-9]{3,4}-[0-9]{4}$/;
    if(!pattern.test(fld.value)){
        if(wrestFld == null){
            wrestMsg = wrestItemname(fld)+" : 电话号码格式不正确:.\n\n连字符(-)请包括.\n";
            wrestFld = fld;
            fld.select();
        }
    }
}

// 电子邮件地址格式检查
function wrestEmail(fld)
{
    if (!wrestTrim(fld)) return;

    //var pattern = /(\S+)@(\S+)\.(\S+)/; 使用韩语作为电子邮件地址时
    var pattern = /^([0-9a-zA-Z_\-\.]+)@([0-9a-zA-Z_\-\.]+)\.([0-9a-zA-Z_\-]+)$/
    if (!pattern.test(fld.value)) {
        if (wrestFld == null) {
            wrestMsg = wrestItemname(fld) + " : 电子邮件地址格式不是.\n";
            wrestFld = fld;
        }
    }
}

// 韩语识别检查 (声音的声音, 只有带有元音组合的韩文是可能的)
function wrestHangul(fld)
{
    if (!wrestTrim(fld)) return;

    //var pattern = /([^对-힣\x20])/i;
    var pattern = /([^对-힣\x20])/;

    if (pattern.test(fld.value)) {
        if (wrestFld == null) {
            wrestMsg = wrestItemname(fld) + ' : 不是朝鲜文. (声音的声音, 只有带有元音组合的韩文是可能的)\n';
            wrestFld = fld;
        }
    }
}

// 韩语识别检查2 (声音的声音, 只有vowes的韩文也是可能的)
function wrestHangul2(fld)
{
    if (!wrestTrim(fld)) return;

    var pattern = /([^对-힣ㄱ-ㅎㅏ-ㅣ\x20])/i;
    //var pattern = /([^对-힣ㄱ-ㅎㅏ-ㅣ\x20])/;

    if (pattern.test(fld.value)) {
        if (wrestFld == null) {
            wrestMsg = wrestItemname(fld) + ' : 不是朝鲜文.\n';
            wrestFld = fld;
        }
    }
}

// 朝鲜文,英语,数字和识别测试3
function wrestHangulAlNum(fld)
{
    if (!wrestTrim(fld)) return;

    var pattern = /([^对-힣\x20^a-z^A-Z^0-9])/i;

    if (pattern.test(fld.value)) {
        if (wrestFld == null) {
            wrestMsg = wrestItemname(fld) + ' : 朝鲜文, 英语, 这不是一个数字.\n';
            wrestFld = fld;
        }
    }
}

// 朝鲜文,英语识别检查
function wrestHangulAlpha(fld)
{
    if (!wrestTrim(fld)) return;

    var pattern = /([^对-힣\x20^a-z^A-Z])/i;
    var pattern = /([^가-힣\x20^a-z^A-Z])/i;

    if (pattern.test(fld.value)) {
        if (wrestFld == null) {
            wrestMsg = wrestItemname(fld) + ' : 朝鲜文, 不是英语.\n';
            wrestFld = fld;
        }
    }
}

// 数字和识别测试
// 添加蜂蜜蜂蜜 (http://dasir.com) 2003-06-24
function wrestNumeric(fld)
{
    if (fld.value.length > 0) {
        for (i = 0; i < fld.value.length; i++) {
            if (fld.value.charAt(i) < '0' || fld.value.charAt(i) > '9') {
                wrestMsg = wrestItemname(fld) + " : 这不是一个数字.\n";
                wrestFld = fld;
            }
        }
    }
}

// 英语字符考试
// 添加蜂蜜蜂蜜 (http://dasir.com) 2003-06-24
function wrestAlpha(fld)
{
    if (!wrestTrim(fld)) return;

    var pattern = /(^[a-zA-Z]+$)/;

    if (!pattern.test(fld.value)) {
        if (wrestFld == null) {
            wrestMsg = wrestItemname(fld) + " : 不是英语.\n";
            wrestFld = fld;
        }
    }
}

// 英文字母和数字检查
// 添加蜂蜜蜂蜜 (http://dasir.com) 2003-07-07
function wrestAlNum(fld)
{
   if (!wrestTrim(fld)) return;

   var pattern = /(^[a-zA-Z0-9]+$)/;

   if (!pattern.test(fld.value)) {
       if (wrestFld == null) {
           wrestMsg = wrestItemname(fld) + " : 不是英文或数字.\n";
           wrestFld = fld;
       }
   }
}

// 英文字母和数字 _ 检查
function wrestAlNum_(fld)
{
   if (!wrestTrim(fld)) return;

   var pattern = /(^[a-zA-Z0-9\_]+$)/;

   if (!pattern.test(fld.value)) {
       if (wrestFld == null) {
           wrestMsg = wrestItemname(fld) + " : 英语, 编号, _ 不是..\n";
           wrestFld = fld;
       }
   }
}

// 最小长度检查
function wrestMinLength(fld)
{
    if (!wrestTrim(fld)) return;

    var minlength = fld.getAttribute("minlength");

    if (wrestFld == null) {
        if (fld.value.length < parseInt(minlength)) {
            wrestMsg = wrestItemname(fld) + " : 最小值 "+minlength+"请输入多个字母.\n";
            wrestFld = fld;
        }
    }
}

// 图像扩展
function wrestImgExt(fld)
{
    if (!wrestTrim(fld)) return;

    var pattern = /\.(gif|jpg|png)$/i; // jpeg 除了
    if(!pattern.test(fld.value)){
        if(wrestFld == null){
            wrestMsg = wrestItemname(fld)+" : 它不是一个图像文件.\n.gif .jpg .png 只有文件是可能的.\n";
            wrestFld = fld;
            fld.select();
        }
    }
}

// 分机
function wrestExtension(fld, css)
{
    if (!wrestTrim(fld)) return;

    var str = css.split("="); // ext=?? <-- str[1]
    var src = fld.value.split(".");
    var ext = src[src.length - 1];

    if (wrestFld == null) {
        if (ext.toLowerCase() < str[1].toLowerCase()) {
            wrestMsg = wrestItemname(fld) + " : ."+str[1]+" 只有文件是可能的.\n";
            wrestFld = fld;
        }
    }
}

// 空白支票后 "" 转换为
function wrestNospace(fld)
{
    var pattern = /(\s)/g; // \s 空格字符

    if (pattern.test(fld.value)) {
        if (wrestFld == null) {
            wrestMsg = wrestItemname(fld) + " : 不应该有空格.\n";
            wrestFld = fld;
        }
    }
}

// submit 检查属性时.
function wrestSubmit()
{
    wrestMsg = "";
    wrestFld = null;

    var attr = null;

    // 返回该表单的元素数
    for (var i=0; i<this.elements.length; i++) {
        var el = this.elements[i];

        // Input tag 的 type 这个 text, file, password 只有当
        // 即使它是选择框，也必须检查选择. select-one
        if (el.type=="text" || el.type=="hidden" || el.type=="file" || el.type=="password" || el.type=="select-one" || el.type=="textarea") {
            if (el.getAttribute("required") != null) {
                wrestRequired(el);
            }

            if (el.getAttribute("minlength") != null) {
                wrestMinLength(el);
            }

            var array_css = el.className.split(" "); // class 分成空间

            el.style.backgroundColor = wrestFldDefaultColor;

            // 转动数组的长度
            for (var k=0; k<array_css.length; k++) {
                var css = array_css[k];
                switch (css) {
                    case "required"     : wrestRequired(el); break;
                    case "trim"         : wrestTrim(el); break;
                    case "email"        : wrestEmail(el); break;
                    case "hangul"       : wrestHangul(el); break;
                    case "hangul2"      : wrestHangul2(el); break;
                    case "hangulalpha"  : wrestHangulAlpha(el); break;
                    case "hangulalnum"  : wrestHangulAlNum(el); break;
                    case "nospace"      : wrestNospace(el); break;
                    case "numeric"      : wrestNumeric(el); break;
                    case "alpha"        : wrestAlpha(el); break;
                    case "alnum"        : wrestAlNum(el); break;
                    case "alnum_"       : wrestAlNum_(el); break;
                    case "telnum"       : wrestTelNum(el); break; // 金善勇 2006.3 - 电话号码格式检查
                    case "imgext"       : wrestImgExt(el); break;
                    default :
                        if (/^extension\=/.test(css)) {
                            wrestExtension(el, css); break;
                        }
                } // switch (css)
            } // for (k)
        } // if (el)
    } // for (i)

    // 该字段是 null 在输出错误消息后将焦点移动到相应的错误字段，如果不是
    // 错误字段更改背景颜色.
    if (wrestFld != null) {
        // 警告信息输出
        alert(wrestMsg);

        if (wrestFld.style.display != "none") {
            var id = wrestFld.getAttribute("id");

            // 对于错误消息 element 添加
            var msg_el = document.createElement("strong");
            msg_el.id = "msg_"+id;
            msg_el.className = "blind";
            msg_el.innerHTML = wrestMsg;
            wrestFld.parentNode.insertBefore(msg_el, wrestFld);

            var new_href = document.location.href.replace(/#msg.+$/, "")+"#msg_"+id;

            document.location.href = new_href;

            //wrestFld.style.backgroundColor = wrestFldBackColor;
            if (typeof(wrestFld.select) != "undefined")
                wrestFld.select();
            wrestFld.focus();
        }
        return false;
    }

    if (this.oldsubmit && this.oldsubmit() == false)
        return false;

    return true;
}


// 在早期 onsubmit我们会设法拦截你。.
function wrestInitialized()
{
    for (var i = 0; i < document.forms.length; i++) {
        // onsubmit 如果有一个事件，保存它。.
        if (document.forms[i].onsubmit) {
            document.forms[i].oldsubmit = document.forms[i].onsubmit;
        }
        document.forms[i].onsubmit = wrestSubmit;
    }
}

// 泡沫现场自动检测
